package com.kuanter.kuanterauto.net;

import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.kuanter.kuanterauto.entity.MapResult;

/**
 * 网络异步任务
 * 
 * @author soup
 * 
 */
public class HttpTask extends AsyncTask<String, Integer, String> {

	// NetHandle handle = null;

	// private String json = "";

	// 请求模式s
	// private int type = 0;
	// public static int POST = 1;
	// public static int GET = 2;
	// public static int PSOT_JSON = 3;
	// public static int PSOT_UTF = 4;
	// public static int PSOT_VALUE = 5;

	private Handler handler = null;

	public static final int RESULT_OK = 1;
	public static final int RESULT_ERROR = -1;

	List<NameValuePair> params = null;
	
	public static String proxyHost;
	public static  int proxyPort;
	public Context context;
	MapResult mapR=null;
	String key="";

	public HttpTask(Handler handler,List<NameValuePair> params,Context context) {
		// handle = NetHandle.getHandle();
		this.handler = handler;
		this.params = params;
		this.context=context;
		mapR=MapResult.getMapResults();
	}

	// public void setJson(String json) {
	// this.json = json;
	// }

	// public void setType(int type) {
	// this.type = type;
	// }

//	public void setParams(List<NameValuePair> params) {
//		this.params = params;
//	}
	
	public void setKey(String key){
		this.key=key;
	}

	@Override
	protected String doInBackground(String... url) {
		String result = "";
		System.out.println(url[0]);
		
		HttpClient client = QHttpClientUtils.getHttpClient(context);
		HttpPost post = new HttpPost(url[0]);
		HttpResponse respons = null;
		try {

			if (this.params != null) {
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
						this.params, HTTP.UTF_8);
				post.setEntity(entity);
			}
			try {
				respons = client.execute(post);
			} catch (Exception e) {
//				post.abort();
				respons = client.execute(post);
				e.printStackTrace();
			}
			if (respons.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				result = EntityUtils.toString(respons.getEntity());
				// response.
				return result;
			} else {
				post.abort();
				result = "网络连接错误";
				return result;
			}
		} catch (Exception e) {
			post.abort();
			result = "网络连接错误";
			e.printStackTrace();
		} 
//		finally {
//			try {
//				respons.getEntity().consumeContent();
////				((AndroidHttpClient) client).close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//			 
//		}

		return result;

	}

	protected void onCancelled() {
		super.onCancelled();
	}
	

	protected void onPostExecute(String result) {
		// System.out.println(result);
		// handle.setResult(result);
		Message msg = new Message();
		Bundle bundle = new Bundle();
		if (result.equals("网络连接错误")) {
			msg.what = RESULT_ERROR;
			handler.sendMessage(msg);
		} else {
			bundle.putString("result", result);
			msg.what = RESULT_OK;
			msg.setData(bundle);
			handler.sendMessage(msg);
		}
		super.onPostExecute(result);
	}

	protected void onPreExecute() {
		// Message msg = new Message();
		// msg.what = RESULT_ERROR;
		// handler.sendMessage(msg);
		super.onPreExecute();
	}

	protected void onProgressUpdate(Integer... values) {

		super.onProgressUpdate(values);
	}

}
